package com.zrrd.yunchmall.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.user.entity.Menu;
import com.zrrd.yunchmall.user.service.IMenuService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "权限服务-菜单模块")
@SuppressWarnings("all")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @ApiOperation("查询菜单数")
    @GetMapping("/treeList")
    public ResponseResult treeList(){
        List<Menu>treeList=menuService.getTreeList();
        return new ResponseResult<>(200,"查询成功",treeList);
    }
    @ApiOperation("查询菜单列表")
    @GetMapping("/list/{parentId}")
    public ResponseResult list(@PathVariable("parentId") Integer parentId,
                               @RequestParam int pageNum,@RequestParam int pageSize){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        return new ResponseResult<>(200,"查询成功",menuService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
    @ApiOperation("是否显示菜单")
    @PostMapping("/updateHidden/{id}")
    public ResponseResult updateHidden(@PathVariable Integer id,@RequestParam Integer hidden){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("hidden",hidden);
        updateWrapper.in("id",id);
        menuService.update(updateWrapper);
        return new ResponseResult<>(200,"修改成功!",updateWrapper);
    }
    @ApiOperation("编辑菜单")
    @GetMapping("/{id}")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        return new ResponseResult<>(200,"查询成功",menuService.getById(id));
    }
    @ApiOperation("修改菜单")
    @PostMapping("/update/{id}")
    public ResponseResult update(@PathVariable("id") Integer id,@RequestBody Menu menu){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        return new ResponseResult<>(200,"查询成功",menuService.update(menu,updateWrapper));
    }
    @ApiOperation("添加菜单")
    @PostMapping("/create")
    public ResponseResult create(@RequestBody Menu menu){
        return new ResponseResult<>(200,"查询成功",menuService.save(menu));
    }
    @ApiOperation("添加菜单")
    @PostMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id")Integer id){
        return new ResponseResult<>(200,"查询成功",menuService.removeById(id));
    }
}













